﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Global;

namespace Eassets.UI.GlobalReference
{
    public partial class WorkFlowDocumentForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                textBoxWorkFlowDocumentCode.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
                textBoxWorkFlowDocumentCode.Attributes.Add("onKeyPress", "javascript:return validateCodeForSymbols(event)");
                textBoxDescription.Attributes.Add("onChange", "javascript:this.value=toTitleCase(this.value)");

              
                GetWorkFlowDocuments();
                ViewState["ButtonBehaviour"] = "Load";
                DisableComponants();
            }

        }

        protected void gridViewWorkFlowDocument_SelectedIndexChanged(object sender, EventArgs e)
        {
            {
                GridViewRow row = gridViewWorkFlowDocument.SelectedRow;
                hiddenFieldWorkFlowDocumentCodeId.Value = row.Cells[1].Text;
                textBoxWorkFlowDocumentCode.Text = row.Cells[2].Text;
                textBoxDescription.Text = row.Cells[3].Text;

                ViewState["ButtonBehaviour"] = "GridSelectedChanged";
                //accordionInputs.SelectedIndex = 1;
            }
        }

        protected void gridViewWorkFlowDocument_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (CheckMandatory("A"))
            {
               WorkFlowDocument workflowdoc = new WorkFlowDocument();
                WorkFlowDocumentService workflowdocservice = new WorkFlowDocumentService();
                workflowdoc.Code = textBoxWorkFlowDocumentCode.Text.ToString();
                workflowdoc.Description = textBoxDescription.Text.ToString();

                try
                {
                    if (Page.IsValid)
                    {
                        workflowdocservice.AddWorkFlowDocument(workflowdoc);
                        FlashMessage1.Message = "Successfully Saved";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Saving Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }
                GetWorkFlowDocuments();
                ClearInputs();
            }

        }

        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            ClearInputs();
            //textBoxWorkFlowDocumentCodeAdd.Text = "";
            //textBoxDescriptionAdd.Text = "";
        }

        protected void buttonSaveEdit_Click(object sender, EventArgs e)
        {
            if (hiddenFieldWorkFlowDocumentCodeId.Value != String.Empty)
            {
                if (CheckMandatory("M"))
                {
                    WorkFlowDocument workflowdoc = new WorkFlowDocument();
                    WorkFlowDocumentService workflowdocservice = new WorkFlowDocumentService();
                    workflowdoc.WorkFlowDocumentId = Convert.ToInt64(hiddenFieldWorkFlowDocumentCodeId.Value);
                    workflowdoc.Code = textBoxWorkFlowDocumentCode.Text.ToString();
                    workflowdoc.Description = textBoxDescription.Text.ToString();

                    try
                    {
                        if (Page.IsValid)
                        {
                            workflowdocservice.ModifyWorkFlowDocument(workflowdoc);
                            FlashMessage1.Message = "Successfully Saved";
                            FlashMessage1.CssClass = "okmessage";
                            FlashMessage1.Display();
                        }
                    }
                    catch (Exception exception)
                    {
                        FlashMessage1.Message = "Saving Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                    }
                    GetWorkFlowDocuments();
                    ClearInputs();
                }
            }
            else
            {
                FlashMessage1.Message = "Select a record to Modify";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }
        }

        protected void buttonDelete_Click(object sender, EventArgs e)
        {
            if (hiddenFieldWorkFlowDocumentCodeId.Value != String.Empty)
            {
                WorkFlowDocumentService workflowdocservice = new WorkFlowDocumentService();

                try
                {
                    if (Page.IsValid)
                    {
                        workflowdocservice.DeleteWorkFlowDocument(Convert.ToInt64(hiddenFieldWorkFlowDocumentCodeId.Value));
                        FlashMessage1.Message = "Successfully Deleted";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Deleting Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                GetWorkFlowDocuments();
                ClearInputs();
            }
            else
            {
                FlashMessage1.Message = "Select a record to Delete";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }
        }

        protected void buttonCancelEdit_Click(object sender, EventArgs e)
        {
            ClearInputs();
            //textBoxWorkFlowDocumentCodeEdit.Text = "";
            //textBoxDescriptionEdit.Text = "";
        }

        private Boolean CheckMandatory(string addOrModify)
        {
            if (addOrModify == "A")
            {
                if (textBoxWorkFlowDocumentCode.Text == String.Empty)
                {
                    FlashMessage1.Display("[Work Flow Document Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }
            else if (addOrModify == "M")
            {
                if (textBoxWorkFlowDocumentCode.Text == String.Empty)
                {
                    FlashMessage1.Display("[Work Flow Document Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }
            return true;
        }

        private void GetWorkFlowDocuments()
        {
            WorkFlowDocument workflowdoc = new WorkFlowDocument();
            WorkFlowDocumentService workflowdocservice = new WorkFlowDocumentService();

            try
            {
                gridViewWorkFlowDocument.DataSource = workflowdocservice.GetAllWorkFlowDocument();
                gridViewWorkFlowDocument.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void ClearInputs()
        {
            textBoxWorkFlowDocumentCode.Text = "";
            textBoxDescription.Text = "";
            textBoxWorkFlowDocumentCode.Text = "";
            textBoxDescription.Text = "";
            hiddenFieldWorkFlowDocumentCodeId.Value = "";
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Add";
            ViewState["checkAddModify"] = true;
            DisableComponants();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Modify";
            ViewState["checkAddModify"] = false;
            DisableComponants();
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {

        }

        //protected void ButtonSave_Click(object sender, EventArgs e)
        //{

        //}

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Cancel";
            DisableComponants();
        }

        protected void DisableComponants()
        {
            if (ViewState["ButtonBehaviour"] == "Add")
            {
                buttonModify.Enabled = false;

                buttonDelete.Enabled = false;

                textBoxWorkFlowDocumentCode.Enabled = true;
                textBoxWorkFlowDocumentCode.Text = String.Empty;
                textBoxDescription.Enabled = true;
                textBoxDescription.Text = string.Empty;
            }
            else if (ViewState["ButtonBehaviour"] == "Modify")
            {
                buttonAdd.Enabled = false;
                buttonDelete.Enabled = false;
                textBoxWorkFlowDocumentCode.Enabled = true;
                textBoxDescription.Enabled = true;

            }
            else if (ViewState["ButtonBehaviour"] == "Delete")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Save")
            {
                textBoxWorkFlowDocumentCode.Enabled = false;
                textBoxWorkFlowDocumentCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "Cancel")
            {
                textBoxWorkFlowDocumentCode.Enabled = false;
                textBoxWorkFlowDocumentCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;
            }

            else if (ViewState["ButtonBehaviour"] == "Load")
            {
                textBoxWorkFlowDocumentCode.Enabled = false;
                textBoxWorkFlowDocumentCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "GridSelectedChanged")
            {
                buttonAdd.Enabled = false;
                buttonModify.Enabled = true;
                buttonDelete.Enabled = true;
            }

        }
    }
}